package blog.action;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import blog.entity.Options;
import blog.entity.User;
import blog.service.OptionsService;
import blog.service.UserService;

/**
 * 处理页面中的图片显示
 */
@Controller
public class ImgAction {
    @Resource
    private OptionsService optionsService;

    @Resource
    private UserService userService;

    /**
     * 提取出站点信息的的图片数据,传到前台展示,由于有站点头象,还有微信图片,所以前面要传一个 photoType 区分一下 (值为sitephoto 或   webchartphoto )
     */
    @RequestMapping("/options/photo/{photoType}")
    public void showOptionsPhoto(@PathVariable("photoType") String photoType, HttpServletResponse response) throws IOException {

        Options options = optionsService.getOptions();
        response.setContentType("image/jpg");
        ServletOutputStream outstream = response.getOutputStream();

        if ("sitephoto".equals(photoType)) {
            outstream.write(options.getOptionAboutsitePhoto());
        } else {
            outstream.write(options.getOptionAboutsiteWechatphoto());
        }

        outstream.flush();
    }

    /**
     * 显示用户的头象信息(比如在言章详细页中)
     */
    @RequestMapping("/user/photo/{id}")
    public void showUserPhoto(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException {
        User user = userService.getUserById(id);
        response.setContentType("image/jpg");
        ServletOutputStream outstream = response.getOutputStream();
        outstream.write(user.getUserPhoto());
        outstream.flush();
    }

}
